package com.deyuanyun.pic.docmger.controller;

import com.deyuanyun.pic.common.dto.PageJsonBean;
import com.deyuanyun.pic.common.util.StringUtils;
import com.deyuanyun.pic.common.util.ajax.AjaxSupport;
import com.deyuanyun.pic.common.util.web.Asserts;
import com.deyuanyun.pic.docmger.controller.dto.DocPipelineDTO;
import com.deyuanyun.pic.docmger.controller.dto.DocPipelineQueryVO;
import com.deyuanyun.pic.docmger.domain.DocPipeline;
import com.deyuanyun.pic.docmger.service.DocPipelineService;
import com.deyuanyun.pic.settings.controller.BaseController;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.MessageFormat;
import java.util.List;

/**
 *  文档中心---管道相关controller
 *  @author axiao(xj08.dream@gmail.com)
 *  @date 2016-11-14
 *  @version 1.0
 *  Histroy: [修改人][时间][描述]
 */
@Controller
@RequestMapping("/docmger/pipeline")
public class DocPipelineController extends BaseController {

    @Autowired
    private DocPipelineService docPipelineService;

    //从左到右依次是：竣工资料、设计文件、检测报告、评价报告以及其它
    private List<String> pageNames = Lists.newArrayList("CompletionData", "DesignDocument", "TestReport", "EvaluationReport", "Others");

    /**
     * 跳转到管道相关页面
     * @param
     * @return 
     * @author axiao(xj08.dream@gmail.com)
     * @date 2016-11-16
     */
    @RequestMapping("/to{pageName}.html")
    public String toCompletionData(@PathVariable("pageName") String pageName, Model model) {
        Asserts.assertTrue(pageNames.contains(pageName), "参数非法！");
        pageName = StringUtils.toLowerCaseFirstOne(pageName);
        model.addAttribute("type", pageName.toUpperCase());
        return MessageFormat.format("/docCenter/{0}", pageName);
    }

    /**
     * 跳转到编辑管道相关信息页面
     * @param
     * @return
     * @author axiao(xj08.dream@gmail.com)
     * @date 2016-11-16
     */
    @RequestMapping("/edit{pageName}.html")
    public String editCompletionData(Model model, @PathVariable("pageName") String type,
                                     @RequestParam(value = "id", required = false) String id) {
        Asserts.assertTrue(pageNames.contains(type), "参数非法！");
        String pageName = StringUtils.toLowerCaseFirstOne(type);
        if(StringUtils.isNotEmpty(id)) {
            DocPipelineDTO docPipelineDTO = docPipelineService.selectByIdType(id, type.toUpperCase());
            model.addAttribute("pipeLine", docPipelineDTO);
        }
        model.addAttribute("type", type.toUpperCase());
        model.addAttribute("basicType", type);
        return MessageFormat.format("/docCenter/{0}Edit", pageName);
    }

    /**
     * 查询管道相关文档信息，分页
     * @param queryVO
     * @return
     * @author axiao(xj08.dream@gmail.com)
     * @date 2016-11-14
     */
    @RequestMapping("/search.json")
    @ResponseBody
    public void search(DocPipelineQueryVO queryVO) {
        PageJsonBean jsonBean = new PageJsonBean();
        jsonBean.setList(docPipelineService.queryPipelineInfo(queryVO));
        AjaxSupport.sendSuccessText("success",StringUtils.DATE, jsonBean);
    }

    /**
     * 根据id删除管道相关文档信息
     * @param id
     * @return
     * @author axiao(xj08.dream@gmail.com)
     * @date 2016-11-14
     */
    @RequestMapping("/delete.json")
    @ResponseBody
    public void deleteStandard(@RequestParam("id") String id) {
        docPipelineService.deletePipelineInfo(id);
        AjaxSupport.sendSuccessText("success", "删除成功！");
    }

    /**
     * 根据id更新管道相关文档信息
     * @param docPipeline
     * @param attachmentId 附件id
     * @return
     * @author axiao(xj08.dream@gmail.com)
     * @date 2016-11-14
     */
    @RequestMapping("/update.json")
    @ResponseBody
    public void updatePipeLineInfo(DocPipeline docPipeline, String attachmentId) {
        docPipelineService.updatePipelineInfo(docPipeline, attachmentId);
        AjaxSupport.sendSuccessText("success", "更新成功！");
    }

    /**
     * 新增管道相关文档信息
     * @param docPipeline
     * @param attachmentId 附件id
     * @return
     * @author axiao(xj08.dream@gmail.com)
     * @date 2016-11-14
     */
    @RequestMapping("/add.json")
    @ResponseBody
    public void addPipeLineInfo(DocPipeline docPipeline, String attachmentId) {
        docPipelineService.addPipelineInfo(docPipeline, attachmentId);
        AjaxSupport.sendSuccessText("success", "更新成功！");
    }

}
